package com.xyht.medicine_management_system.modules.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.classification.entity.MmsMedicineClassification;
import com.xyht.medicine_management_system.modules.classification.mapper.MmsMedicineClassificationMapper;
import com.xyht.medicine_management_system.modules.manufacturer.entity.MmsManufacturer;
import com.xyht.medicine_management_system.modules.manufacturer.mapper.MmsManufacturerMapper;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicine;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicineHot;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicineSpecial;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineHotAddListReq;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineHotAddReq;
import com.xyht.medicine_management_system.modules.medicine.entity.req.MmsMedicineHotDeletedReq;
import com.xyht.medicine_management_system.modules.medicine.entity.resp.*;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineHotMapper;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineMapper;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineSpecialMapper;
import com.xyht.medicine_management_system.modules.medicine.service.MmsMedicineHotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.medicine_management_system.modules.user.entity.MmsUser;
import com.xyht.medicine_management_system.modules.user.mapper.MmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.classification.constant.QualificationConstant.QUALIFICATION_NONE;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsSpecialConstant.MEDICINE_IS_NOT_SPECIAL;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsSpecialConstant.MEDICINE_IS_SPECIAL;
import static com.xyht.medicine_management_system.modules.user.constant.QualificationTypeConstant.QUALIFICATION_TYPE_NONE;

/**
 * <p>
 * 热销药品 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-12-04
 */
@Service
public class MmsMedicineHotServiceImpl extends ServiceImpl<MmsMedicineHotMapper, MmsMedicineHot> implements MmsMedicineHotService {

    @Resource
    private MmsMedicineHotMapper medicineHotMapper;
    @Resource
    private MmsMedicineMapper medicineMapper;
    @Resource
    private MmsMedicineSpecialMapper medicineSpecialMapper;
    @Resource
    private MmsMedicineClassificationMapper medicineClassificationMapper;
    @Resource
    private MmsManufacturerMapper manufacturerMapper;
    @Resource
    private MmsUserMapper userMapper;


    @Override
    public ResponseResult addMedicineHot(MmsMedicineHotAddListReq addListReq, String userId) {
        if (addListReq.getMedicineList() == null || addListReq.getMedicineList().size() < 1) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        //校验药品
        List<MmsMedicineHotAddReq> medicineList = addListReq.getMedicineList();

        for (MmsMedicineHotAddReq mmsMedicineHotAddReq : medicineList) {
            MmsMedicine medicine = medicineMapper.selectById(mmsMedicineHotAddReq.getMedicineId());
            if (isNullOrEmpty(medicine)) {
                return CommonResult.failed(CommonCodeEnum.MEDICINE_NOT_EXIST);
            }
        }

        //没有就填
        for (MmsMedicineHotAddReq mmsMedicineHotAddReq : medicineList) {
            List<MmsMedicineHot> mmsMedicineHots = medicineHotMapper.selectList(new LambdaQueryWrapper<MmsMedicineHot>()
                    .in(MmsMedicineHot::getMedicineId, mmsMedicineHotAddReq.getMedicineId()));
            if (mmsMedicineHots.isEmpty()) {
                MmsMedicineHot medicineHot = new MmsMedicineHot();
                medicineHot.setMedicineId(mmsMedicineHotAddReq.getMedicineId());
                medicineHot.setDescription(mmsMedicineHotAddReq.getDescription());
                medicineHot.setUserId(userId);
                medicineHotMapper.insert(medicineHot);
            }
        }

        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedMedicineHot(MmsMedicineHotDeletedReq deletedReq) {
        if (deletedReq.getIdList() == null || deletedReq.getIdList().size() < 1) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<String> idList = deletedReq.getIdList();
        for (String id : idList) {
            MmsMedicineHot medicineHot = medicineHotMapper.selectById(id);
            if (isNullOrEmpty(medicineHot)) {
                return CommonResult.failed(CommonCodeEnum.MEDICINE_HOT_NOT_EXIST);
            }
        }

        medicineHotMapper.deleteBatchIds(idList);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getMedicineHotList(Integer pageNum, Integer pageSize, String name, Integer isOn, String userId) {
        Page<MmsMedicineHot> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<MmsMedicineHot> medicineHotLambdaQueryWrapper = new LambdaQueryWrapper<MmsMedicineHot>().orderByDesc(MmsMedicineHot::getCreateTime);
        LambdaQueryWrapper<MmsMedicineClassification> classificationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<MmsMedicine> medicineLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //构造检验药品分类资质条件
        boolean flag = false;
        if (!isNullOrEmpty(userId)) {
            MmsUser mmsUser = userMapper.selectById(userId);
            if (mmsUser != null) {
                flag = true;

            }
        }
        //1.flag为true时有 用户登录及用户信息
        if (flag) {
            //查用户资质
            MmsUser mmsUser = userMapper.selectById(userId);
            Integer qualificationType = mmsUser.getQualificationType();
            //用户没有资质时,只能查看没有资质分类的药品
            if (qualificationType == QUALIFICATION_TYPE_NONE) {
                //不需要资质的分类
                List<MmsMedicineClassification> classificationList = medicineClassificationMapper.selectList(
                        classificationLambdaQueryWrapper.eq(MmsMedicineClassification::getIsQualification, qualificationType));
                if (!classificationList.isEmpty()) {
                    List<String> classificationIdList = classificationList.stream().map(MmsMedicineClassification::getId).collect(Collectors.toList());
                    if (!classificationIdList.isEmpty()) {
                        medicineLambdaQueryWrapper.in(MmsMedicine::getClassificationId, classificationIdList);
                    }else {
                        medicineLambdaQueryWrapper.in(MmsMedicine::getClassificationId, "");
                    }
                    List<MmsMedicine> medicineList = medicineMapper.selectList(medicineLambdaQueryWrapper);
                    List<String> medicineIdList = medicineList.stream().map(MmsMedicine::getId).collect(Collectors.toList());
                    if (!medicineIdList.isEmpty()) {
                        medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, medicineIdList);
                    } else {
                        medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, "");
                    }

                }else {
                    medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId,"");
                }
            }
        //2.flag为false时没有 用户登录及用户信息
        }else {
            List<MmsMedicineClassification> classificationList = medicineClassificationMapper.selectList(classificationLambdaQueryWrapper
                    .eq(MmsMedicineClassification::getIsQualification, QUALIFICATION_NONE));
            if (!classificationList.isEmpty()) {
                List<String> classificationIdList = classificationList.stream().map(MmsMedicineClassification::getId).collect(Collectors.toList());
                if (!classificationIdList.isEmpty()) {
                    medicineLambdaQueryWrapper.in(MmsMedicine::getClassificationId, classificationIdList);
                }else {
                    medicineLambdaQueryWrapper.in(MmsMedicine::getClassificationId, "");
                }
                List<MmsMedicine> medicineList = medicineMapper.selectList(medicineLambdaQueryWrapper);
                List<String> medicineIdList = medicineList.stream().map(MmsMedicine::getId).collect(Collectors.toList());
                if (!medicineIdList.isEmpty()) {
                    medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, medicineIdList);
                } else {
                    medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, "");
                }

            }else {
                medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId,"");
            }
        }

        if (!isNullOrEmpty(name)) {
            List<String> medicineIdList = medicineMapper.selectList(new LambdaQueryWrapper<MmsMedicine>()
                    .like(MmsMedicine::getCommonNames, name))
                    .stream().map(MmsMedicine::getId).collect(Collectors.toList());
            if (medicineIdList.size() > 1) {
                medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, medicineIdList);
            } else {
                medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, "");
            }
        }

        if (!isNullOrEmpty(isOn)) {
            List<String> medicineIdList = medicineMapper.selectList(new LambdaQueryWrapper<MmsMedicine>()
                    .eq(MmsMedicine::getIsOn, isOn))
                    .stream().map(MmsMedicine::getId).collect(Collectors.toList());
            if (medicineIdList.size() > 1) {
                medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, medicineIdList);
            } else {
                medicineHotLambdaQueryWrapper.in(MmsMedicineHot::getMedicineId, "");
            }
        }

        medicineHotMapper.selectPage(page, medicineHotLambdaQueryWrapper);
        List<MmsMedicineHotResp> medicineHotList = page.getRecords()
                .stream()
                .map(mmsMedicineHot -> {
                    MmsMedicineHotResp medicineHotResp = new MmsMedicineHotResp();
                    MmsMedicineDetailResp medicineDetailResp = new MmsMedicineDetailResp();
                    BeanUtils.copyProperties(mmsMedicineHot, medicineHotResp);
                    MmsMedicine medicine = medicineMapper.selectById(mmsMedicineHot.getMedicineId());
                    if (!isNullOrEmpty(medicine)) {
                        BeanUtils.copyProperties(medicine, medicineDetailResp);

                        //分类
                        MmsMedicineClassification medicineClassification = medicineClassificationMapper.selectById(medicine.getClassificationId());
                        if (!isNullOrEmpty(medicineClassification)) {
                            MmsMedicineClassificationResp classificationResp = new MmsMedicineClassificationResp();
                            BeanUtils.copyProperties(medicineClassification, classificationResp);
                            medicineDetailResp.setClassificationInfo(classificationResp);
                        }
                        //厂家
                        MmsManufacturer mmsManufacturer = manufacturerMapper.selectById(medicine.getManufacturer());
                        if (!isNullOrEmpty(mmsManufacturer)) {
                            MmsMedicineManufacturerResp manufacturerResp = new MmsMedicineManufacturerResp();
                            BeanUtils.copyProperties(mmsManufacturer, manufacturerResp);
                            medicineDetailResp.setManufacturerInfo(manufacturerResp);
                        }

                    }

                    //判断是否特惠商品
                    List<MmsMedicineSpecial> specialList = medicineSpecialMapper.selectList(new LambdaQueryWrapper<MmsMedicineSpecial>()
                            .eq(MmsMedicineSpecial::getMedicineId, mmsMedicineHot.getMedicineId()));
                    if (specialList != null && specialList.size() > 0) {
                        medicineDetailResp.setIsSpecial(MEDICINE_IS_SPECIAL);
                        medicineDetailResp.setSpecialPrice(specialList.get(0).getSpecialPrice());
                    } else {
                        medicineDetailResp.setIsSpecial(MEDICINE_IS_NOT_SPECIAL);
                    }

                    medicineHotResp.setMedicineInfo(medicineDetailResp);
                    return medicineHotResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(medicineHotList,total);
    }
}
